package com.hxut.servlet;

import com.hxut.dao.UserDao;
import com.hxut.model.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RegisterServlet", urlPatterns = "/registerServlet")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("password");
        String name = req.getParameter("password");
        String phone = req.getParameter("password");
        String address = req.getParameter("address");
/*        String gender = req.getParameter("gender");
        String hobbys[] = req.getParameterValues("hobby");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        System.out.println("gender:"+gender);
        if(hobbys!=null){
            for(int i=0;i<hobbys.length;i++){
                System.out.println("hobbys:"+hobbys[i]);
            }
        }*/
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setName(name);
        user.setPhone(phone);
        user.setAddress(address);
        //通过请求转发方式跳转页面
        if(username==null || username.isEmpty() ||password==null || password.isEmpty()){
        	req.setAttribute("message","用户名或密码错误");
            RequestDispatcher dispatcher =req.getRequestDispatcher("register.jsp");
        	dispatcher.forward(req, resp);
        }else {
            UserDao userDao = new UserDao();
            User user1 = userDao.findUserByUsername(username);

            if(user1!=null && user1.getUsername()!=null &&!user1.getUsername().equals("")){
                req.setAttribute("message","用户名不合法");
                RequestDispatcher dispatcher = req.getRequestDispatcher("/register.jsp");
                dispatcher.forward(req,resp);
            }else {
                int i = userDao.addUser(user);
                if (i>0){ //注册成功
                    RequestDispatcher dispatcher = req.getRequestDispatcher("/login.jsp");
                    dispatcher.forward(req,resp);
                }else {
                    req.setAttribute("message","执行插入操作失败");
                    RequestDispatcher dispatcher = req.getRequestDispatcher("/register.jsp");
                    dispatcher.forward(req,resp);
                }
            }

        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
